﻿Imports System.IO.IsolatedStorage
Imports System.Windows.Forms
Imports Runa.Scripting
Imports System.Text
Imports Runa.Core.GamePackage
Imports System.Threading.Tasks

Public Class FormGameChooser
    Public Property GameName() As String

    Private Sub StartGame(ByVal name As String)
        Me.GameName = name
        Me.Hide()
        Dim thr As New System.Threading.Thread(New Threading.ParameterizedThreadStart(
            Sub(gn)
                Try
                    Using game As New GameRender(g_gameList.Where(Function(obj) obj.Name = gn.ToString())(0))
                        AddHandler game.Shown,
                            Sub()
                                Me.BeginInvoke(
                                    Sub()
                                    Me.Hide()
                                End Sub)
                            End Sub
                        game.Run()
                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace.ToString())
                End Try
                Application.Exit()
            End Sub))
        thr.Start(GameName)
    End Sub

    Private Sub FormGameChooser_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'g_gameList = GetGames()
        If g_gameList.Count > 0 Then
            For Each game In g_gameList
                Me.LB_GameList.Items.Add(game.Name)
            Next
            Me.GameName = g_gameList(0).Name
        End If
    End Sub

    Private Sub FormGameChooser_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        On Error Resume Next
        For i As Double = 1 To 0 Step -0.1
            Me.Opacity = i
            Threading.Thread.Sleep(100)
        Next
        Me.Hide()
        Application.Exit()
    End Sub

    Private Sub Btn_About_Click(sender As System.Object, e As System.EventArgs) Handles Btn_About.Click
        Using frmAbout As New FormLoading
            frmAbout.ShowDialog()
        End Using
    End Sub

    Private Sub Btn_Exit_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Exit.Click
        Application.Exit()
    End Sub

    Private Sub Btn_Load_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Load.Click
        If Me.LB_GameList.SelectedItems.Count > 0 Then
            Me.StartGame(Me.LB_GameList.SelectedItem.ToString())
        Else
            If Me.LB_GameList.Items.Count > 0 Then
                MessageBox.Show("Please choose a game to play!")
            Else
                Try
                    Using ofd As New OpenFileDialog()
                        With ofd
                            .Title = "Open a game"
                            .Filter = "Runa game package(*.rpk)|*.rpk|All files(*.*)|*.*"
                            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                                Dim fs = .OpenFile()
                                Dim name = Path.GetFileNameWithoutExtension(.FileName)
                                Me.PB_Progress.Visible = True
                                Dim thr As New Threading.Thread(
                                    Sub()
                                        Using fake = g_fileStorage.OpenFile(name & ".zip", Runa.Foundation.IO.FileMode.Create)
                                        End Using
                                        Dim pkg = New RunaGamePackage(name, fs, g_fileStorage)
                                        g_gameList.Add(pkg)
                                        Me.BeginInvoke(
                                            Sub()
                                                Me.PB_Progress.Visible = False
                                                Me.StartGame(name)
                                            End Sub)
                                        fs.Close()
                                    End Sub)
                                thr.Start()
                            End If
                        End With
                    End Using
                Catch ex As Exception
                    Debuger.TraceListener.WriteLine(String.Format("{0}: {1}" & vbCrLf, Now.ToString(), ex.Message, ex.StackTrace.ToString()))
                End Try
            End If
        End If
    End Sub

    Private Sub Btn_ViewLog_Click(sender As Object, e As System.EventArgs) Handles Btn_ViewLog.Click
        If Me.LB_GameList.SelectedIndex > -1 Then
            FormViewLog.GameName = Me.LB_GameList.SelectedItem.ToString()
            FormViewLog.ShowDialog(Me)
        End If
    End Sub

    Private Sub StartToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles StartToolStripMenuItem.Click
        Dim lb = CType(Me.CMS.SourceControl, ListBox)
        Me.StartGame(lb.SelectedItem.ToString())
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        Dim lb = CType(Me.CMS.SourceControl, ListBox)
        Me.GameName = lb.SelectedItem.ToString()
        Dim dirs = g_fileStorage.GetDirectories()
        If dirs.Contains(Me.GameName) Then
            If MessageBox.Show("Are you sure to delete this game?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.OK Then
                g_fileStorage.DeleteDirectory(Me.GameName, True)
                g_fileStorage.DeleteFile(Me.GameName & ".zip")
            End If
        End If
    End Sub

    Private Sub PropertyToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles PropertyToolStripMenuItem.Click
        Dim g = g_gameList.Where(Function(obj) obj.Name = Me.GameName).First()
        Dim sb As New StringBuilder
        With g
            sb.AppendLine("Name: " & .Name)
            sb.Append("Author: " & .Author)
            MessageBox.Show(sb.ToString())
        End With
    End Sub

End Class